#include "ogre_gui_test.h"

#include "space\space.h"

#include <OGRE\OgreRoot.h>
#include <OGRE\OgreConfig.h>
#include <OGRE\OgreConfigFile.h>
#include <OGRE\OgreRenderSystem.h>
#include <OGRE\OgreSceneManager.h>
#include <OGRE\OgreCamera.h>
#include <OGRE\OgreViewport.h>
#include <OGRE\OgreRenderWindow.h>
#include <OGRE\OgreEntity.h>
#include <OGRE\OgreSceneNode.h>
#include <OGRE\OgreLight.h>

#include <CEGUI.h>
#include <RendererModules\Ogre\CEGUIOgreRenderer.h>


void ogre_cegui_test(void) {
	CEGUI::Renderer* mGUIRenderer;
	CEGUI::System* mGUISystem;
	CEGUI::Window* mEditorGuiSheet;
	CEGUI::DefaultResourceProvider * mResources;
	CEGUI::WindowManager * mWindowManager;
	CEGUI::Window *mWindow;

	Ogre::Root				* oRoot;
	Ogre::RenderWindow		* oRenderWindow;
	Ogre::Camera			* oCamera;
	Ogre::Viewport			* oViewport;
	Ogre::SceneManager		* oSceneManager;
	Ogre::Entity			* oEntity;
	Ogre::SceneNode			* oSceneNode;

	oRoot = new Ogre::Root("plugins_d.cfg");

	Ogre::ConfigFile cf;
	cf.load("resources_d.cfg");
	Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();

	Ogre::String secName,
		typeName,
		archName;

	LOG("Loading configured resources.")
		while(seci.hasMoreElements())
		{
			secName = seci.peekNextKey();
			Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
			Ogre::ConfigFile::SettingsMultiMap::iterator i;
			for (i = settings->begin(); i != settings->end(); ++i)
			{
				typeName = i->first;
				archName = i->second;
				Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
					archName, typeName, secName);
			}
			LOG("\t%s", secName.c_str());
		}

		LOG("Checking render systems");
		Ogre::RenderSystemList rList = oRoot->getAvailableRenderers();
		Ogre::RenderSystemList::iterator i = rList.begin();
		Ogre::RenderSystem* RenderSys, *SetRenderSys;
		while (i != rList.end()) {
			RenderSys = (*i++);
			LOG("Found render system: [%s]", RenderSys->getName().c_str());
			if(RenderSys->getName().find("Direct3D9 Rendering Subsystem") != std::string::npos) {
				//if(RenderSys->getName().find("Direct3D11 Rendering Subsystem") != std::string::npos) {
				SetRenderSys = RenderSys;
			}
		}

		LOG("Attempting initialization");
		LOG("\tSetting render system: [%s]", SetRenderSys->getName().c_str());
		oRoot->setRenderSystem(SetRenderSys);

		RenderSys->setConfigOption("Video Mode", "1024x768");
		RenderSys->setConfigOption("Full Screen", "No");

		oRoot->showConfigDialog();
		oRoot->saveConfig();

		oRoot->initialise(false);

		oRenderWindow = oRoot->createRenderWindow("TEST TEST TEST", 1024, 768, false);

		LOG("\tCreating scene");
		oSceneManager = oRoot->createSceneManager(Ogre::ST_GENERIC, "Main");
		oSceneManager->setAmbientLight(Ogre::ColourValue(0.5f, 0.5f, 0.5f));

		oCamera = oSceneManager->createCamera("Main Camera");
		oCamera->setPosition(0,0,500);
		oCamera->lookAt(100, 0, 0);

		oViewport = oRenderWindow->addViewport(oCamera);

		oViewport->setBackgroundColour(Ogre::ColourValue::Blue);

		//oEntity = oSceneManager->createEntity("Ninja", "ninja.mesh");
		//oSceneNode = oSceneManager->getRootSceneNode()->createChildSceneNode("NinjaNode", Ogre::Vector3( 100, 0, 0 ) );
		//oSceneNode->attachObject(oEntity);

		Ogre::Light* light = oSceneManager->createLight("MainLight");
		light->setPosition(20.0f, 80.0f, 50.0f);

		LOG("Beginning CEGUI Configuration...");

		mGUIRenderer = &CEGUI::OgreRenderer::bootstrapSystem(*oRenderWindow);
		CEGUI::Logger::getSingleton().setLoggingLevel(CEGUI::Informative);

		Ogre::ResourceGroupManager& rgm = Ogre::ResourceGroupManager::getSingleton();
		// get the winodwmanger
		mWindowManager = &CEGUI::WindowManager::getSingleton();
		
		rgm.createResourceGroup("imagesets");
		rgm.createResourceGroup("fonts");
		rgm.createResourceGroup("layouts");
		rgm.createResourceGroup("schemes");
		rgm.createResourceGroup("looknfeels");
		rgm.createResourceGroup("lua_scripts");
		rgm.createResourceGroup("schemas");
		rgm.addResourceLocation("S:/SDK/CEGUI-0.7.7/datafiles/schemes/", "FileSystem", "schemes");
		rgm.addResourceLocation("S:/SDK/CEGUI-0.7.7/datafiles/imagesets/", "FileSystem", "imagesets");
		rgm.addResourceLocation("S:/SDK/CEGUI-0.7.7/datafiles/fonts/", "FileSystem", "fonts");
		rgm.addResourceLocation("S:/SDK/CEGUI-0.7.7/datafiles/layouts/", "FileSystem", "layouts");
		rgm.addResourceLocation("S:/SDK/CEGUI-0.7.7/datafiles/looknfeel/", "FileSystem", "looknfeel");
		rgm.addResourceLocation("S:/SDK/CEGUI-0.7.7/datafiles/lua_scripts/", "FileSystem", "lua_scripts");
			
		CEGUI::Imageset::setDefaultResourceGroup("imagesets");
		CEGUI::Font::setDefaultResourceGroup("fonts");
		CEGUI::Scheme::setDefaultResourceGroup("schemes");
		CEGUI::WidgetLookManager::setDefaultResourceGroup("looknfeel");
		CEGUI::WindowManager::setDefaultResourceGroup("layouts");
		CEGUI::ScriptModule::setDefaultResourceGroup("lua_scripts");
		
		CEGUI::SchemeManager::getSingleton().create("VanillaSkin.scheme");
		CEGUI::System::getSingleton().setDefaultMouseCursor("Vanilla-Images", "MouseArrow");
		
		mWindow = mWindowManager->createWindow( "DefaultWindow", "background_wnd" );
		CEGUI::System::getSingleton().setGUISheet( mWindow );
		
		CEGUI::Window * _test_window = CEGUI::WindowManager::getSingleton().loadWindowLayout( "VanillaWindows.layout" );
		mWindow->addChildWindow(_test_window);

		oRenderWindow->setActive(true);
		oRenderWindow->update();

		//oRoot->startRendering();

		LOG("OGRE CEGUI test complete.");
		LOG("Press any key to continue.");
		getchar();

		delete oRoot;

}